/*
  将下面异步代码使用 Promise 的方法改进
  尽量用看上去像同步代码的方式
  setTimeout(function () {
    var a = 'hello'
    setTimeout(function () {
      var b = 'lagou'
      setTimeout(function () {
        var c = 'I ♥ U'
        console.log(a + b +c)
      }, 10)
    }, 10)
  }, 10)
*/

// 常规改造
new Promise((resolve, reject) => {
  setTimeout(_ => resolve('hello '), 10)
}).then(v => {
  return new Promise(resolve => {
    setTimeout(_ => resolve(v + 'lagou '), 10)
  })
}).then(v => {
  return new Promise(resolve => {
    setTimeout(_ => resolve(v + 'I ♥ U'), 10)
  })
}).then(console.log)

// 常规改造简写
new Promise(resolve => setTimeout(_ => resolve('hello '), 10))
  .then(v => new Promise(resolve => setTimeout(_ => resolve(v + 'lagou '), 10)))
  .then(v => new Promise(resolve => setTimeout(_ => resolve(v + 'I ♥ U'), 10)))
  .then(console.log)


// async/await
const getWord = (w, t) => new Promise(resolve => setTimeout(_ => resolve(w), t))

const asyncJoin = async _ => {
  const a = await getWord('hello ', 10)
  const b = await getWord('lagou ', 10)
  const c = await getWord('I ♥ U ', 10)

  console.log(a + b + c)
}

asyncJoin()
